package com.hsnn.medstgmini.base.dict.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.model.DicCompTypeLog;
import com.hsnn.medstgmini.base.dict.service.DicCompTypeLogManager;
import com.hsnn.medstgmini.model.Result;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/dicCompTypeLog")
public class DicCompTypeLogController {
	
	private static final Logger log = Logger.getLogger(DicCompTypeLogController.class);
	
	@Autowired
	private DicCompTypeLogManager dicCompTypeLogManager;
	
	@Autowired
	HttpServletRequest request;
	
	@RequestMapping("log")
	public String dicCompTypeLogList() {
		printParams();
		String id = request.getParameter("compRegId");
		request.setAttribute("id", id);
//		model.addAttribute("id", id);
		request.setAttribute("code", request.getParameter("compRegCode"));
		request.setAttribute("titleCode", request.getParameter("titleCode"));
		try {
			String titleName = new String(request.getParameter("titleName").getBytes("iso8859-1"),"utf-8");
			request.setAttribute("titleName", titleName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "/base/dict/compTypeLogList";
	}
	
	@RequestMapping("jsonTable")
	@ResponseBody
	public Result jsonTable(Result result, DicCompTypeLog dicCompTypeLog) {
		result = result == null ? new Result() : result;
		try {
			List<DicCompTypeLog> list = dicCompTypeLogManager.dicCompTypeLogList(result, dicCompTypeLog);
			result.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setType(Result.FAILED);
		} catch(Error error) {
			error.printStackTrace();
			result.setType(Result.FAILED);
		}
		return result;
	}
	
	/**
	 * 基础库 数据字典 企业注册类型维护记录  查询及条件查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/getDicCompTypeLogList")
	@ResponseBody
	public Pagination getDicCompTypeLogList() {
		printParams();
		Pagination page = new Pagination(request);
		try {
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String compRegId = request.getParameter("compRegId");
			String compRegCode = request.getParameter("compRegCode");
			if(StringUtils.isNotBlank(startTime)){
				page.getConditions().put("startTime",startTime.trim());
			}
			if(StringUtils.isNotBlank(endTime)){
				page.getConditions().put("endTime",endTime.trim());
			}
			page.getConditions().put("compRegId",compRegId.trim());
			if(StringUtils.isNotBlank(compRegCode)){
				page.getConditions().put("compRegCode",compRegCode.trim());
			}
			dicCompTypeLogManager.getDicCompTypeLogList(page);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} catch (Error error) {
			error.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 基础库 数据字典 企业注册类型维护记录 日志详情
	 * @param page
	 * @return
	 */
	@RequestMapping("/toDicCompTypeLogDetail")
	public String toDicHosptypeLogDetail(){
		log.info("enter toDicHosptypeLogDetail method!");
		printParams();
		try {
			Pagination  page = new Pagination();
			page.getConditions().put("logId", request.getParameter("logId"));
			List<Map<String,Object>> list = dicCompTypeLogManager.getDicCompTypeLogDetail(page);
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"一级企业注册类型编码","一级企业注册类型名称","二级企业注册类型编码","二级企业注册类型名称","状态"};
			String[] eColunm = {"fatherCompRegCode","fatherCompRegName","compRegCode","compRegName","status"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("dataList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/base/dict/compTypeLogDetail";
		
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	

}
